﻿using Mapster;
using Service.Auth.Domain.AggregatesModel;
using Service.Auth.Model.Model.Dto;
using Service.Auth.Model.Model.QueryDto;
using Service.Core.Repository;
using Service.Framework;

namespace Service.Auth.Api.Application.Query;

/// <summary>
/// 菜单权限查询器
/// </summary>
/// <param name="permissionRepository"></param>
public class PemissionsQueryService(
    IRepository<Permission> permissionRepository) : IScopedService
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<List<PermissionDto>> QueryPemissions(QueryPemissionsModel request)
    {
        var tree = await permissionRepository.Db.Queryable<Permission>()
            .WhereIF(request.PerantId.HasValue, s => s.ParentId.Equals(request.PerantId)).OrderBy(s => s.OrderSort)
            .ToTreeAsync(s => s.Children, s => s.ParentId, null);
        return tree.Adapt<List<PermissionDto>>();
    }
}